import Room from "./Room";
import LanguageMng from "../Managers/LanguageMng";
import GameLogicUtilTs from "../Utils/GameLogicUtilTs";
import RoomConfig from "../Static/RoomConfig";
import RoomTypes from "../Static/RoomTypes";

export default class PoolRoom extends Room {

    //赌注
    bet = 0;
    //记分板
    scores = [];

    en_class() {
        return 'PoolRoom';
    }

    onCreate(){
        this.info = cc.gg.enumConfig.getRoomInfoById(cc.gg.enumConfig.RoomType.POOL,this.mode)
    }

    initRoom(gameMgr){
        super.initRoom(gameMgr)

        this.onTriggerEvent('scores', (data) => this.onScores(data))
        this.onTriggerEvent('result', (data) => this.onResult(data));

        let oriScores = this.scores
        cc.log("初始化scores")
        this.scores = []

        cc.logfl("初始化得分记录scores",oriScores)
        for(let i=0;i<oriScores.length;i++){
            let netScoreObj = oriScores[i]
            if(!this.scores[netScoreObj.id]){
                this.scores[netScoreObj.id] = {}
                this.scores[netScoreObj.id].scores = []
            }

            this.scores[netScoreObj.id].scores = netScoreObj.scores
        }
        cc.logfl("初始化得分记录scores完成",this.scores)
    }

    onResult(data){
        super.onResult(data)


        cc.logws("PoolRoom 收到result消息：",data)

        let resultArray = data[0]
        resultArray.sort((a, b) => a.score - b.score)

        let localUser = cc.gg.dataMgr.getPlayerUser()
        let isEliminated = localUser.score > cc.gg.dataMgr.room.s_top

        let gameName = ""
        if(cc.gg.dataMgr.room.info.e_fee === 0){
            gameName = "Practise"
        }else {
            gameName = cc.gg.gameDataUtil.getStringOfRupee(cc.gg.dataMgr.room.info.e_fee) + "-" + cc.gg.dataMgr.room.s_top
        }

        let contentStr = ""
        if(isEliminated){
            let eliStr1 = "You have reched the maximum score allowed for this game and you have been eliminated.Would you like to play another "
            let eliStr2 = " Pool game?"
            contentStr = eliStr1 + gameName + eliStr2
        }else {
            let winStr1 = "You have won the game! Would you like to play another "
            let winStr2 = " Pool game?"
            contentStr = winStr1 + gameName + winStr2
        }

        setTimeout(function()
        {
            cc.gg.tpgMgr.uiMgr._showDealBoard()
            this.scores = []
        }.bind(this),1000)

        setTimeout(function()
        {
            // let title = LanguageMng.getSentence(3)
            // let positiveStr = "Go Lobby"
            // let passiveStr = "Play Again"
            // cc.gg.toastMgr.showDialog(title,positiveStr,passiveStr,contentStr,
            //     function () {
            //         cc.gg.gameDataUtil.goToLobby(true)
            //     }.bind(this),
            //     function () {
            //         cc.find("Canvas").getComponent("GameMgrBase").againFindRoom()
            //     }
            // )
            let title = LanguageMng.getSentence(3)
            let positiveStr = "OK"
            let passiveStr = "Play Again"
            cc.gg.toastMgr.showDialogOneButton(title,positiveStr,contentStr,
                function () {
                    let user = cc.gg.dataMgr.getPlayerUser()
                    if(user) user.leave()
                    cc.gg.toastMgr.removeDialog()
                }.bind(this)
            )
        }.bind(this),2000)
    }

    onScores(data){
        this.updateScores(data)
        // this.scores = data
    }

    onReport(data){
        if(!cc.gg.gameDataUtil.receiveFinish){
            let showScoreWhenWaiting = true
            cc.find("Canvas/UIManager").emit("ui_init_deal_info_board",null,null,showScoreWhenWaiting)
        }

        super.onReport(data)

        let scores = data[0]
        this.updateScores(scores)
        this.updateWinner(scores)
    }


    /**
     * can be used to points and deal
     * @param scores
     */
    updateWinner(scores){
        cc.log("开始更新Pool局赢家：",scores)

        let smallest = null
        for(let i in scores){
            let tmpUser = scores[i]
            let id = tmpUser.id
            let score = tmpUser.score

            // let userObj = cc.gg.dataMgr.getUser(id)
            // if(!userObj) continue
            //
            // if(!biggestUser){
            //     biggestUser = userObj
            //     continue
            // }
            if(!smallest){
                smallest = tmpUser
                continue
            }

            smallest = tmpUser.score < smallest.score ? tmpUser : smallest
        }
        cc.log("赢家是：",smallest.id)

        cc.find("Canvas/UIManager").emit("ui_update_dealinfo_winner",smallest.id)
    }

    onDestroy(){
        super.onDestroy()
        cc.find("Canvas").emit("game_room_destroy")
        // if(GameLogicUtilTs.wantQuitRoom){
        //     GameLogicUtilTs.wantQuitRoom = false
        //     cc.find("Canvas").emit("game_room_destroy")
        // }
    }

    updateScores(scores){
        cc.log("poolScores增加记录：",scores)
        for(let i=0;i<scores.length;i++){
            let netScoreObj = scores[i]
            if(!this.scores[netScoreObj.id]){
                this.scores[netScoreObj.id] = {}
                this.scores[netScoreObj.id].scores = []
            }

            this.scores[netScoreObj.id].scores.push(netScoreObj.score)
        }
        cc.log("poolScores增加记录完毕：",this.scores)
    }
}